// pages/list/search/index.js
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import * as Storage from '../../../../utils/storage';
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],

    // 呼叫器id
    beeperId: '',
    //分页相关
    pageNum: 1,
    pageSize: '',
    userRole: 0,
    // 订单状态
    orderStatusList: [
      { text: '订单状态', value: '' },
      { text: '待接单', value: 1 },
      { text: '已接单', value: 5 },
      { text: '已确认', value: 10 },
      { text: '已出发', value: 11 },
      { text: '已到达', value: 12 },
      { text: '已取消', value: 15 },
      { text: '取消失败', value: 16 },
      { text: '已完成', value: 20 }
    ],

    //  下单时间
    orderDateList: [
      { text: '下单时间', value: '' },
      { text: '今日', value: 1 },
      { text: '3天内', value: 3 },
      { text: '7天内', value: 7 },
      { text: '一个月内', value: 30 },
      { text: '两个月内', value: 60 },
      { text: '三个月内', value: 90 }
    ],
    orderScoreList: [
      { text: '评分等级', value: '' },
      { text: '五星评分', value: 5 },
      { text: '四星及以下', value: 4 }
    ],
    orderState: '',
    timeDate: '',
    totalScore: '',
    orderNo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 用户的时候会传过来呼叫器
    if (options.beeperId) {
      this.setData({
        beeperId: options.beeperId
      });
    }
    let userRole = wx.getStorageSync(Storage.USER_ROLE);
    this.setData({ userRole: userRole });
    // this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  // 获取列表数据
  getData() {
    let params = {
      pageNo: this.data.pageNum,
      pageSize: 10,
      orderState: this.data.orderState,
      date: this.data.timeDate,
      totalScore: this.data.totalScore,
      beeperId: this.data.beeperId,
      orderNo: this.data.orderNo
    };
    $api
      .orderList(params)
      .then(res => {
        console.log('获取订单', res);
        let data = res.data.data;
        this.setData({
          dataList: data.list,
          pageNum: data.pageNum,
          pageSize: data.pageSize
        });
      })
      .catch(err => {
        console.log('获取订单', err);
      });
  },
  // 订单编号改变
  orderNoChange(e) {
    console.log(e);
    this.setData({
      orderNo: e.detail
    });
  },
  // 订单编号搜索
  searchOrder() {
    this.setData({ pageNum: 1 });
    this.getData();
  },
  // 订单状态选择筛选
  statusChange({ detail }) {
    this.setData({ orderState: detail, pageNum: 1 });
    this.getData();
  },
  //  下单时间选择筛选
  dateChange({ detail }) {
    this.setData({ timeDate: detail, pageNum: 1 });
    this.getData();
  },
  // 评分等级
  scoreChange({ detail }) {
    this.setData({ totalScore: detail, pageNum: 1 });
    this.getData();
  },
  // 去详情页
  toDetaile(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subcontract/order/pages/details/index?orderId=' + id
    });
  },
  //刷新
  onRefresh() {
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...'
    });
    this.setData({
      pageNum: 1
    });
    this.getData();

    setTimeout(() => {
      wx.showToast({
        title: '刷新成功'
      });
      wx.stopPullDownRefresh();
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      pageNum: this.data.pageNum + 1
    });
    let params = {
      pageNo: this.data.pageNum,
      pageSize: 10,
      orderState: this.data.orderState,
      date: this.data.timeDate,
      totalScore: this.data.totalScore
    };
    let dataList = this.data.dataList;
    var dataConcat = [];
    $api
      .orderList(params)
      .then(res => {
        let list = res.data.data.list;
        console.log('分页出来的list', list);
        if (list.length != 0) {
          dataConcat = dataList.concat(list);
          this.setData({
            dataList: dataConcat
          });
        } else {
          Toast('没有更多啦');
        }
        console.log('合并后', this.data.dataList);
      })
      .catch(err => {
        console.log('充值记录', err);
      });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
